package com.qf.emp.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DbUtils {
    //得到一个数据库连接池
    private static DruidDataSource ds;
    //对外提供一个get方法 可以通过外部访问到DataSource
    public static DataSource getDataSource(){
        return ds;
    }
    //控制事务，来存放连接池对象
    private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();
    //静态代码块，进行一些初始化工作
    static{
        //通过容器保存配置文件信息，
        Properties properties = new Properties();
        //得到一个字节输入流
        InputStream is = DbUtils.class.getResourceAsStream("/database.properties");
        try {
            //加载配置
            properties.load(is);
            //创建数据库连接池。通过连接池工厂，创建一个连接池
            ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection connection = THREAD_LOCAL.get();
        try {
            if (connection == null){
                connection = ds.getConnection();
                THREAD_LOCAL.set(connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    // 开启事务，创建连接，开启自动提交事务
    public static void begin(){
        Connection connection = null;
        try {
            connection = getConnection();
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    // 事务内部都执行成功了，执行提交。
    public static void commit(){
        Connection connection = null;
        try {
            connection = getConnection();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(connection,null,null);
        }
    }
    // 如果事务内部出现错误，进行回滚。
    public static void rollback(){
        Connection connection = null;
        try {
            connection = getConnection();
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(connection,null,null);
        }
    }
    //关闭所有连接的对象，遵循先开后开的原则
    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if(resultSet != null){
                resultSet.close();
            }
            if(statement != null){
                statement.close();
            }
            if(connection != null){
                connection.close();
                THREAD_LOCAL.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
